package au.com.handsunited.web.controller.organisation;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.mvc.SimpleFormController;

import au.com.handsunited.model.Organisation;
import au.com.handsunited.service.OrganisationService;
import au.com.handsunited.service.ServiceCategoryService;
import au.com.handsunited.utils.constants.DateConstants;
import au.com.handsunited.utils.constants.RegionConstants;
import au.com.handsunited.web.command.organisation.OrganisationCommand;

/**
 * @author arunc
 *
 */
public abstract class AbstractOrganisationEditController extends
		SimpleFormController {

	protected OrganisationService organisationService;
	protected ServiceCategoryService serviceCategoryService;
	protected DateConstants dateConstants;
	protected RegionConstants regionConstants;


	private static Logger logger = LoggerFactory.getLogger(AbstractOrganisationEditController.class);


	/**
	 *
	 */
	public AbstractOrganisationEditController() {
		setCommandClass(OrganisationCommand.class);
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("countries", regionConstants.getCountries());
		data.put("states", regionConstants.getStates());
		data.put("days", dateConstants.getDays());
		data.put("months", dateConstants.getMonths());
		data.put("years", dateConstants.getFutureYears());
		data.put("hours", dateConstants.getHours());
		data.put("minutes", dateConstants.getMinutes());
        data.put("serviceCategories", serviceCategoryService.getAllServiceCategories());

		return data;
	}


	/**
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {

		OrganisationCommand command = null;

		if (StringUtils.isNotBlank(request.getParameter("id"))) {
			Long organisationId = Long.parseLong(request.getParameter("id"));
			Organisation organisation = organisationService.getOrganisation(organisationId);
			command = new OrganisationCommand(organisation);
		} else {
			command = new OrganisationCommand();
		}

		command.setServiceCategoryMap(serviceCategoryService.getServiceCategoryMap());

		return command;
	}


	/**
	 * @param organisationService the organisationService to set
	 */
	public void setOrganisationService(OrganisationService organisationService) {
		this.organisationService = organisationService;
	}

	/**
	 * @param serviceCategoryService the serviceCategoryService to set
	 */
	public void setServiceCategoryService(
			ServiceCategoryService serviceCategoryService) {
		this.serviceCategoryService = serviceCategoryService;
	}

	/**
	 * @param dateConstants the dateConstants to set
	 */
	public void setDateConstants(DateConstants dateConstants) {
		this.dateConstants = dateConstants;
	}

	/**
	 * @param regionConstants the regionConstants to set
	 */
	public void setRegionConstants(RegionConstants regionConstants) {
		this.regionConstants = regionConstants;
	}

}
